package com.example.test_aidl;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author Ohuang
 */
public class MyService extends Service {
    private static final String TAG = "MyService";
    private User user0 = new User();
    List<User> userList = new ArrayList<>();
    private List<AidlInterface> mAidlInterfaces = new ArrayList<>();

    private IMyAidlInterface.Stub userlmpl = new IMyAidlInterface.Stub() {

        @Override
        public List<User> getUsers() throws RemoteException {
            synchronized (this) {
                Log.e(TAG, "invoking getUsers() method , now the list is : " + userList.toString());
                if (userList != null) {
                    return userList;
                }
                return new ArrayList<>();
            }
        }

        //In 服务改变数据 客户端数据不改变
        @Override
        public User addUserIn(User user) throws RemoteException {
            synchronized (this) {
                if (userList == null) {
                    userList = new ArrayList<>();
                }
                if (user == null) {
                    Log.e(TAG, "Book is null in In");
                    user = new User();
                }
                for (AidlInterface aidlInterface :
                        mAidlInterfaces) {
                    aidlInterface.onMessge("add User:" + user.toString());
                }
                //尝试修改book的参数，主要是为了观察其到客户端的反馈
                user.setMsg("有钱了");
                if (!userList.contains(user)) {
                    userList.add(user);
                }

                Log.e(TAG, "invoking addUserIn() method , now the list is : " + user.toString());
                return user;
            }
        }

        //out  user结束到的值为null   但是改变数据 客户端数据可以改变
        @Override
        public User addUserOut(User user) throws RemoteException {
            synchronized (this) {
                if (userList == null) {
                    userList = new ArrayList<>();
                }
                if (user == null) {
                    Log.e(TAG, "Book is null in In");
                    user = new User();
                }
                for (AidlInterface aidlInterface :
                        mAidlInterfaces) {
                    aidlInterface.onMessge("add User:" + user.toString());
                }
                //尝试修改book的参数，主要是为了观察其到客户端的反馈
                user.setMsg("有钱了");
                if (!userList.contains(user)) {
                    userList.add(user);
                }

                Log.e(TAG, "invoking addUserOut() method , now the list is : " + user.toString());
                return user;
            }
        }

        //inout 同时满足int 和 out两种情况
        @Override
        public User addUserInout(User user) throws RemoteException {
            synchronized (this) {
                if (userList == null) {
                    userList = new ArrayList<>();
                }

                if (user == null) {
                    Log.e(TAG, "Book is null in In");
                    user = new User();
                }
                for (AidlInterface aidlInterface :
                        mAidlInterfaces) {
                    aidlInterface.onMessge("add User:" + user.toString());
                }
                //尝试修改book的参数，主要是为了观察其到客户端的反馈
                user.setMsg("有钱了");
                if (!userList.contains(user)) {
                    userList.add(user);
                }

                Log.e(TAG, "invoking addUserInout() method , now the list is : " + user.toString());
                return user;
            }
        }

        @Override
        public void setUsersMsg(String msg) throws RemoteException {
            for (User user:
                 userList) {
                user.setMsg(msg);
            }
        }

        //添加AIDL接口用于回调
        @Override
        public void addAidlInterface(AidlInterface a) throws RemoteException {
            mAidlInterfaces.add(a);
        }


        @Override
        public void removeAidlInterface(AidlInterface a) throws RemoteException {
            mAidlInterfaces.remove(a);
        }


        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

        }
    };

    public MyService() {
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "onBind: ");
        return userlmpl;
    }

}


